package ru.urfc.portal.developer_tasks.ejb_tests;

import java.math.BigDecimal;
import java.util.Date;

import javax.ejb.EJB;

import ru.urfc.portal.dao.BalanceDao;
import ru.urfc.portal.dao.SpectrId;
import ru.urfc.portal.developer_tasks.BaseTestEjb;
import ru.urfc.portal.model.Branch;
import ru.urfc.portal.model.accounting.Balance;
import ru.urfc.portal.model.accounting.Contragent;
import ru.urfc.portal.model.accounting.InvoiceFacture;

public class BalanceDaoTestEjb extends BaseTestEjb {

	@EJB BalanceDao balanceDao;

	Contragent contragent;
	SpectrId contragentId;
	
	@Override
	protected void setUp() throws Exception {
		contragent = persist(new Contragent());
		contragentId = new SpectrId(contragent.getId(), Branch.SVERD);
	}
	
	public void testGetLastBalanceBefore() {
		persist(new Balance(contragent, 2000, 10, new BigDecimal(100)));
		Balance foundBalance = balanceDao.getLastBalanceBefore(contragentId, 2000, 10, 1);
		assertEquals(null, foundBalance);
	}
	
	public void testGetLastBalanceBeforeYear() {
		persist(new Balance(contragent, 1999, 10, new BigDecimal(100)));
		Balance balance = persist(new Balance(contragent, 2000, 10, new BigDecimal(100)));
		persist(new Balance(contragent, 1999, 10, new BigDecimal(100)));
		Balance foundBalance = balanceDao.getLastBalanceBefore(contragentId, 2001, 10, 1);
		assertEquals(balance.getId(), foundBalance.getId());
	}
	
	public void testGetLastBalanceBeforeMonth() {
		Balance balance = persist(new Balance(contragent, 2000, 10, new BigDecimal(100)));
		Balance foundBalance = balanceDao.getLastBalanceBefore(contragentId, 2000, 11, 1);
		assertEquals(balance.getId(), foundBalance.getId());
	}
	
	public void testGetLastBalanceBeforeYearMonth() {
		Balance balance = persist(new Balance(contragent, 2000, 10, new BigDecimal(100)));
		Balance foundBalance = balanceDao.getLastBalanceBefore(contragentId, 2001, 5, 1);
		assertEquals(balance.getId(), foundBalance.getId());
	}
	
	public void testFindInInterval() {
		Date beforeFromDate =	new Date(0);
		Date fromDate =		new Date(100000000L);
		Date toDate =		new Date(200000000L);
		Date afterToDate =	new Date(300000000L);
		persist(new InvoiceFacture(contragent, "", "", beforeFromDate, null));
		persist(new InvoiceFacture(contragent, "", "", fromDate, null));
		persist(new InvoiceFacture(contragent, "", "", afterToDate, null));
		assertEquals(1, balanceDao.getInvoiceFactures(contragentId, fromDate, toDate).size());
		assertEquals(2, balanceDao.getInvoiceFactures(contragentId, fromDate, null).size());
	}
	
	public void testGetBalances() {
		Integer fromYear =	1999;
		Integer fromMonth =	5;
		Integer toYear =	2006;
		Integer toMonth =	7;
		persist(new Balance(contragent, fromYear + 1, fromMonth, new BigDecimal(100)));
		assertEquals(1, balanceDao.getBalances(contragentId, fromYear, fromMonth, 1, toYear, toMonth, 1).size());
		persist(new Balance(contragent, fromYear, fromMonth, null));
		persist(new Balance(contragent, toYear, toMonth, null));
		assertEquals(3, balanceDao.getBalances(contragentId, fromYear, fromMonth, 1, toYear, toMonth, 1).size());
		persist(new Balance(contragent, fromYear, fromMonth - 1, null));
		persist(new Balance(contragent, toYear, toMonth + 1, null));
		persist(new Balance(contragent, fromYear - 1, fromMonth, null));
		persist(new Balance(contragent, toYear + 1, toMonth, null));
		assertEquals(3, balanceDao.getBalances(contragentId, fromYear, fromMonth, 1, toYear, toMonth, 1).size());
	}
	
}
